<?php
/*------------------------------------------------------------------------------
Copyright (C) 2006 J.Ducastel fredbird@fredbird.org

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
------------------------------------------------------------------------------*/

/**
* Instances of this classes are handles for mysql host links, one instance per host.
* It may be used to open databases, carrying queries, and tracking errors and results.
* @author J.Ducastel <fredbird@fredbird.org>
* @version 
* @use dbresult_mysql
*/
class dbLink_mysql {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	*
	* @access private
	*/
	function dbLink_mysql($host) {
		$this->__construct($host);
	}/**/
	
	/**
	*
	* @access private
	*/
	function __construct($host) {
		$this->host=$host;
	}/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/**
	* return host-associated instance
	* @static
	* @param string $host
	* @access public
	* @return dblink_mysql
	*/
	function & getInstance($host) {
		// checking param
		
		// building instance if necessary
		static $instances;
		if (!isset($instances[$host])) {
			$instances[$host]=new dbLink_mysql($host);
		}
		return $instances[$host];
	}/**/
	
	/**
	* connect user to host
	* @param string $login
	* @param string $pwd
	* @access public
	* @return bool
	*/
	function connectUser($login,$pwd='') {
		if (!$this->link=@mysql_connect($this->host,$login,$pwd)) {
			trigger_error("dbLink_mysql->connectUser couldn't connect to host ".$this->host." ".strlen($login)."/".strlen($pwd),E_USER_WARNING);
			return false;
		}
		$this->login=$login;
		$this->pwd=$pwd;
		// auto-closing
		register_shutdown_function('mysql_close');
		return true;
	}/**/
	
	/**
	* sets database
	* @param
	* @access public
	* @return 
	*/
	function setDb($db) {
		if (!$this->link) {
			trigger_error('dbLink_mysql->setDb: no link',E_USER_NOTICE);
			return false;
		} else if (!@mysql_select_db($db, $this->link)) {
			trigger_error('dbLink_mysql->setDb: failed to select '.$db,E_USER_WARNING);
			return false;
		}
		$this->db=$db;
		return true;
	}/**/
	
	/**
	* sets encoding for db transactions
	* @param string encoding
	* @access public
	* @return bool
	*/
	function setEncoding($encoding) {
		// MySQL 4.0
		if (!$this->getSqlResource("SET CHARACTER SET '$encoding'")) {
			trigger_error('dbLink_mysql->setEncoding: failed to set encoding '.$encoding,E_USER_WARNING);
			return false;
		}
		// MySQL 4.1
		if (!$this->getSqlResource("SET NAMES '$encoding'")) {
			trigger_error('dbLink_mysql->setEncoding: failed to set encoding '.$encoding,E_USER_WARNING);
			return false;
		}
		$this->encoding=$encoding;
		// echo "<p>encoding $encoding set for db transactions</p>";
		return true;
	}/**/
	
	/**
	*
	* @return resource
	*/
	function & getSQLResource($sql,$buffered=false) {
		if (!$this->link) {
			trigger_error("dbLink_mysql->query: no link to execute query",E_USER_NOTICE);
			$r=false;
		} else if (!$this->db) {
			trigger_error("dbLink_mysql->query: no database selected to execute query",E_USER_NOTICE);
			$r=false;
		// execute query
		} else if ($buffered) {
			$r=@mysql_query($sql,$this->link);
		} else {
			$r=@mysql_unbuffered_query($sql,$this->link);
		}
		return $r;
	}
	
	/**
	* execute a query, logs it and returns result
	* @param string $sql
	* @param bool $buffered (default false) if result is buffered
	* @access public
	* @return dbResult_mysql
	*/
	function & getDBResult($sql, $buffered=false) {
		$r=$this->getSqlResource($sql,$buffered);
		// logs result, error etc
		if ($r) {
			// success
			$result=&new dbresult_mysql($r,$sql,$buffered);
		} else {
			// failed
			$result=&new dbresult_mysql($r,$sql,$buffered
				,mysql_errno($this->link),mysql_error($this->link));
		}
		$this->results[]=&$result;
		return $result;
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	/**
	* @var type desc
	*/
	var $host;
	/**
	* @var type desc
	*/
	var $link;
	/**
	* @var type desc
	*/
	var $encoding;
	/**
	* @var type desc
	*/
	var $login;
	/**
	* @var type desc
	*/
	var $pwd;
	/**
	* @var type desc
	*/
	var $db;
	/**
	* @var type desc
	*/
	var $results=array();
	
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
	/**
	* 
	* @param
	* @access protected
	* @return 
	* /
	function () {
		
	}/**/
	
	/**
	* 
	* @param
	* @access protected
	* @return 
	* /
	function () {
		
	}/**/
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
	/**
	* 
	* @param
	* @access private
	* @return 
	* /
	function () {
		
	}/**/
	
	/**
	* 
	* @param
	* @access private
	* @return 
	* /
	function () {
		
	}/**/
}
?>
